#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb
import json
import sys
import urllib2

def get_actoin():
    conn = MySQLdb.connect(host="keepfitdb.mysql.rds.aliyuncs.com",user="for_ol",passwd="Abcd1234",port=3306)
    cur = conn.cursor()
    conn.select_db('coacht_base')
    cur.execute('select id,np_video from t_action')
    rows = cur.fetchall()
    cur.close()
    conn.close()
    return rows

if __name__ == "__main__":
    actions = get_actoin()
    conn = MySQLdb.connect(host="keepfitdb.mysql.rds.aliyuncs.com",user="for_ol",passwd="Abcd1234",port=3306)
    cur = conn.cursor()
    conn.select_db('coacht_base')
    for action in actions:
        print action
        url = action[1]
        if url is None or url.strip()=='':
            continue
        url = url+"?stat"
        jsonData =  urllib2.urlopen(url).read()
        map = json.loads(jsonData)
        #print map['hash']
        sql = "update t_action set new_hash = '"+map['hash']+"' where id = "+str(action[0])
        cur.execute(sql)
        print sql;
    #print actions
    conn.commit()
    cur.close()
    conn.close()
    sys.exit()
